import { Data } from '../../../utils/data.js';
const con = require("../../../utils/data.js");
const app = getApp()
const CONFIG = require('../../../config.js')
const WXAPI = require('../../../wxapi/main')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: ['江西省', '南昌市', '青山湖区'],
        dianhua: '请填写电话',
        kefu:'社区客服',
        youxiang: '请填写邮箱',
      cName: '请输入姓名',
        index: '0',
        picker: ['请选择性别', '男', '女'],
        date: '2019-08-15',
        dengji: '0',
        balance: 0.00,
        freeze: 0,
        countUserCollect:0,
        score: 0,
        openid:'',
        score_sign_continuous: 0,
        cashlogs: undefined,
        viplevel: '', //会员等级
        myscore: '',
        user:[]
    },
    DateChange(e) {
        this.setData({
            date: e.detail.value
        })
    },
    //  表单验证
    bindSave(e) {
        console.log(e);
        // this.setData({
        //   // kefu: e.detail.value.kefu,
        //   cGender: e.detail.value.cGender,
        //   cPhone: e.detail.value.cPhone,
        //   cEmail: e.detail.value.cEmail ,
        //   cAddress: e.detail.value.cAddress,
        // });
        // 提交表单
        let openid = this.data.openid;
        let cAddress = e.detail.value.cAddress[0] + ',' + e.detail.value.cAddress[1] + ',' + e.detail.value.cAddress[2];
        // var birthday = 'e.detail.value.birthday';
        // console.log(birthday);
        var params = {}

        const reg = /^1[3|4|6|5|7|8][0-9]\d{8}$/;
        if (!reg.test(e.detail.value.cPhone)) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none'
            })
            return
        } else {
            params.cPhone = e.detail.value.cPhone
        }
        if (e.detail.value.cName) {
            params.cName = e.detail.value.cName
        } else {
            wx.showToast({
                title: '请输入真实姓名',
                icon: 'none'
            });
            return
        }
        if (e.detail.value.cAddress[0]) {
            params.cAddress = cAddress
        } else {
            wx.showToast({
                title: '请输入地址',
                icon: 'none'
            })
            return
        }
        if (this.data.index != '0') {
            params.cGender = this.data.index
        } else {
            wx.showToast({
                title: '请选择性别',
                icon: 'none'
            })
            return
        }
        if (e.detail.value.cEmail) {
            params.cEmail = e.detail.value.cEmail
        } else {
            wx.showToast({
                title: '请输入邮箱',
                icon: 'none'
            })
            return
        }
        if (e.detail.value.birthday && e.detail.value.birthday.indexOf("请选择生日") == -1) {
            params.birthday = e.detail.value.birthday
        } else {
            wx.showToast({
                title: '请输入生日',
                icon: 'none'
            })
            return
        }
        params.openid = openid
        console.log(params)
        wx.request({
            url: con.index_setData,
            // url:'http://192.168.0.103:8080/wechat/editData',
            data: params,
            // data: {
            //     'cGender': e.detail.value.cGender,
            //     'cAddress': cAddress,
            //     'cPhone': e.detail.value.cPhone,
            //     'cEmail': e.detail.value.cEmail,
            //     'birthday': e.detail.value.birthday,
            //     'openid': openid,
            //     // 'birthday': e.detail.value.birthday
            // },
            method: 'GET',
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                console.log(res.data);
                console.log('返回成功');
                if (res.data.status) {
                    wx.showToast({
                        title: '保存成功',
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none'
                    })
                }

            },
            fail: function(res) {
                console.log('返回失败');
            }
        })
    },

    RegionChange: function(e) {
        this.setData({
            region: e.detail.value
        })
    },
    bindAgreeChange: function(e) {
        this.setData({
            isAgree: !!e.detail.value.length
        });
    },
    showTopTips: function() {
        var that = this;
        this.setData({
            showTopTips: true
        });
        setTimeout(function() {
            that.setData({
                showTopTips: false
            });
        }, 3000);
    },
    PickerChange(e) {
        this.setData({
            index: e.detail.value
        })
        console.log(this.data.index)
    },

    // 获取头像名称
    onLoad(options) {
        let openid = options.openid
        let that = this
        wx.request({
                url: con.index_getData,
                // url: 'http://192.168.0.103:8080/wechat/getData',
                data: {
                    'openid': openid,
                },
                method: 'GET',
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res.data);
                  var customers = res.data.body.customers;
                  var countUserCollect = res.data.body.countUserCollect;
                  let region = customers.cAddress.split(",");
                    console.log('返回成功');
                  if (customers.viplevel == 0) {
                        var viplevel = "白银"
                    }
                  if (customers.viplevel == 1) {
                        var viplevel = "铂金"
                    }
                  if (customers.viplevel == 2) {
                        var viplevel = "钻石"
                    }
                    that.setData({
                      'dianhua': customers.cPhone,
                      'date': (res.data.body.birthday ? res.data.body.birthday.split(' ')[0] : false) || '请选择生日',
                      'youxiang': customers.cEmail,
                        'region': region,
                      'index': customers.cGender || '0',
                        'viplevel': viplevel,
                        'openid':openid,
                        'user':customers,
                      'countUserCollect': countUserCollect,
                      'cName': customers.cName, 
                    });
                },
                fail: function(res) {
                    console.log('返回失败');
                }
            })
            // 获取积分
        wx.request({
            // url: "https://www.brvpmusic.com/sign_space/parameter",
            url: 'https://www.brvpmusic.com/integral_space/balance',
            data: {
                'cOpenid': openid,
            },
            method: 'GET',
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                console.log(res.data);
                that.setData({
                        'myscore': res.data.body
                    })
                    // console.log(typeof (a));
                    // console.log("a:"+a);
            },
            fail: function(res) {
                console.log('返回失败');
                wx.showToast({
                    title: '未签到不能查看',
                    icon: 'loading',
                    duration: 1500
                });
            }

        });
    },
    onShow() {
        let that = this;
        let userInfo = wx.getStorageSync('userInfo')
        if (!userInfo) {
            app.goLoginPageTimeOut()
        } else {
            that.setData({
                userInfo: userInfo,
                version: CONFIG.version,
                vipLevel: app.globalData.vipLevel
            })
        }
        // this.getUserApiInfo();
        // this.getUserAmount();
    },

    getPhoneNumber: function(e) {
        if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
            wx.showModal({
                title: '提示',
                content: '无法获取手机号码:' + e.detail.errMsg,
                showCancel: false
            })
            return;
        }
        var that = this;
        WXAPI.bindMobile({
            token: wx.getStorageSync('token'),
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
        }).then(function(res) {
            if (res.code === 10002) {
                app.goLoginPageTimeOut()
                return
            }
            if (res.code == 0) {
                wx.showToast({
                        title: '绑定成功',
                        icon: 'success',
                        duration: 2000
                    })
                    // that.getUserApiInfo();
            } else {
                wx.showModal({
                    title: '提示',
                    content: '绑定失败',
                    showCancel: false
                })
            }
        })
    },
    getUserApiInfo: function() {
        var that = this;
        WXAPI.userDetail(wx.getStorageSync('token')).then(function(res) {
            if (res.code == 0) {
                let _data = {}
                _data.apiUserInfoMap = res.data
                if (res.data.base.mobile) {
                    _data.userMobile = res.data.base.mobile
                }
                that.setData(_data);
            }
        })
    },
    getUserAmount: function() {
        var that = this;
        WXAPI.userAmount(wx.getStorageSync('token')).then(function(res) {
            if (res.code == 0) {
                that.setData({
                    balance: res.data.balance.toFixed(2),
                    freeze: res.data.freeze.toFixed(2),
                    score: res.data.score
                });
            }
        })
    },
    relogin: function() {
        app.goLoginPageTimeOut()
    },
    goScore() {
        wx.navigateTo({
            url: "/pages/score/index"
        })
    },
    goCollection() {
        wx.navigateTo({
          url: "/pages/user_goods_collection/index"
        })
    }


})